package cn.edu.pku.service;

import cn.edu.pku.entity.BookCategory;
import cn.edu.pku.mapper.BookCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 图书列别service
 * Created by jinlong.liu on 2017/5/9.
 */
@Service
public class BookCategoryService {
    @Autowired
    BookCategoryMapper bookCategoryMapper;

    public List<BookCategory> getAllCategory() {
        return bookCategoryMapper.selectAllCategory();
    }

    public int addBookCategory(String bookcategoryName) {
        BookCategory category = new BookCategory();
        category.setName(bookcategoryName);
        category.setcTime(new Date());
        return bookCategoryMapper.insert(category);
    }

    public int deleteBookCategory(int id){
        return bookCategoryMapper.deleteByPrimaryKey(id);
    }

    public int updateBookCategory(int id, String name) {
        BookCategory category = bookCategoryMapper.selectByPrimaryKey(id);
        if (category!=null){
            category.setName(name);
            category.setuTime(new Date());
            return bookCategoryMapper.updateByPrimaryKeySelective(category);
        }else {
            return 0;
        }

    }

    public List<BookCategory> getCategoryByName(String name) {
        return bookCategoryMapper.getCategoryByName(name);
    }
}
